//
//  Database.swift
//  SitPosition
//
//  Created by apple on 2019/2/10.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import SQLite


/**
 * SQLite数据库
 */
struct Database {
    
    // 主键类型
    enum PrimaryKey {
        case rowid(rowid: Int64)
        case custom(key: String)
    }
    
    // 全局单例
    public static let shared = Database()
    
    // 数据库
    public var db: SQLite.Connection?
    
    // 数据表的自增长ID
    static let rowId = Expression<Int64>("id")
    
    init() {
        do {
            let path: String = try Paths.dbPath()
            self.db = try SQLite.Connection(path)
            guard let db_ = self.db else { return }
            try UploadTask.createIfNeeded(db_)
            try ChatUserCacheInfo.createIfNeeded(db_)
        } catch {
            dbgPrint("ERROR: failed to init database: \(error)")
            self.db = nil
        }
    }
}
